package sskj.regulatory.presentation.screen.authentication.video


import io.reactivex.android.schedulers.AndroidSchedulers
import isskj.regulatory.presentation.base_mvp.api.ApiPresenter
import sskj.regulatory.data.response.session.IndexDataResponse
import sskj.regulatory.domain.entity.user.BingDingEntity
import sskj.regulatory.domain.fetcher.result_listener.RequestType
import sskj.regulatory.presentation.constans.Constans
import javax.inject.Inject

class VideoPresenter @Inject constructor()
    : ApiPresenter<VideoContract.View>(), VideoContract.Presenter {


    private var indexData: IndexDataResponse? = null

    override fun indexData() {
        fetch(userInteractor.getIndexData()) {
            indexData = it
            when (it.data?.bingDings?.isEmpty()) {
                false -> view.initOk(mapper.tanslate(Constans.JK_ROLE_ID, it.data.bingDings))
                else -> view.initOk(null)
            }
        }
    }

    override fun onPresenterCreate() {
        super.onPresenterCreate()
        indexData()
    }

    override fun bingDing(username: String, password: String) {

        view.showLoading()

        fetch(userInteractor.bingDing(BingDingEntity(null, indexData?.data?.simpleUser?.id, Constans.JK_ROLE_ID,
                Constans.JK_ROLE_NAME, username, username, password)),
                RequestType.BINGDING) {
            view.hideLoading()
            when (it.success) {
                true -> {
                    indexData()
                    view.bingDingOk()
                }
                else -> view.showError(it.msg)
            }
        }
    }

    override fun delBingDing() {
        view.showLoading()
        fetch(userInteractor.delBingDing(indexData?.getBingDingByRole(Constans.JK_ROLE_ID)?.id.toString())) {
            when (it.success) {
                true -> {
                    fetch(userInteractor.getIndexData()) {
                        view.hideLoading()
                        indexData = it
                        view.initOk(null)
                        view.delBingDingOk()
                    }
                }
                else -> view.showError(it.msg)
            }
        }
    }


    override fun auth(username: String, password: String) {
        view.showLoading()
        when (username.isEmpty()) {
            true -> {
                view.showTipDialogError("用户名不能为空")
                return
            }
        }

        run(userInteractor.videoAuth(username, password), AndroidSchedulers.mainThread(), {
            view.hideLoading()
            view.authOk(indexData, username)

        }, {
            view.hideLoading()
            view.showError(it.message)
        })

    }
}